package br.com.northwind.dao;

import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.northwind.database.Conexao;
import br.com.northwind.exceptions.ConexaoException;
import br.com.northwind.exceptions.DriverException;
import br.com.northwind.fabricas.FabricaConexao;
import br.com.northwind.interfaces.ICategoriesDao;
import br.com.northwind.tabelas.Categories;

public class CategoriesDao implements ICategoriesDao {

	private final String SQL_INSERT = "INSERT INTO Categories ( CategoryName, Description, Picture ) VALUES (?,?,?)";
	private final String SQL_UPDATE = "UPDATE Categories SET CategoryName = ?, Description = ?, Picture = ? WHERE CategoryID = ?";
	private final String SQL_DELETE = "DELETE FROM Categories WHERE CategoryID = ?";
	private final String SQL_SELECT_TODOS = "SELECT * FROM Categories";
	private final String SQL_SELECT_BY_CODIGO = "SELECT * FROM Categories WHERE CategoryID = ?";

	private Conexao conexao;
	private PreparedStatement statement = null;
	private ResultSet rs = null;

	public CategoriesDao() {
		this.conexao = FabricaConexao.criarConexao();
	}

	public CategoriesDao(Conexao conexao) {
		this.conexao = conexao;
	}

	public void inserir(Categories _categories) throws ConexaoException,DriverException {
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_INSERT);
			
			this.statement.setString(1, _categories.getCategoryName());
			this.statement.setString(2, _categories.getDescription());
			this.statement.setBlob(3, _categories.getPicture());
			
			this.statement.execute();
		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
	}

	public void editar(Categories _categories) throws ConexaoException,DriverException {
		PreparedStatement update = null;
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_BY_CODIGO);
			
			this.statement.setInt(1, _categories.getCategoryId());
			
			this.rs = statement.executeQuery();

			if (rs.next()) {
				update = this.conexao.getConexao().prepareStatement(this.SQL_UPDATE);

				update.setString(1, _categories.getCategoryName());
				update.setString(2, _categories.getDescription());
				update.setBlob(3, _categories.getPicture());

				update.execute();
				update.close();
			}
		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				if (this.rs != null) this.rs.close();
				if (update != null) update.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}

	}

	public void excluir(Integer _categoryid) throws ConexaoException,DriverException {
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_DELETE);
			
			this.statement.setInt(1, _categoryid);
			
			this.statement.execute();

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				if (this.rs != null) this.rs.close();
				
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
	}

	public List<Categories> selecionarAll() throws ConexaoException,DriverException {
		List<Categories> listaDeCategories = new ArrayList<Categories>();
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_TODOS);
			this.rs = this.statement.executeQuery();

			while (rs.next()) {
				
				int CategoryID = this.rs.getInt(1);
				String CategoryName = this.rs.getString(2);
				String Description = this.rs.getString(3);
				Blob Picture = this.rs.getBlob(4);
				
				listaDeCategories.add(new Categories(CategoryID, CategoryName,Description, Picture));
				
			}

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
		return listaDeCategories;
	}

	public Categories selecionarByCod(Integer _categoryid) throws ConexaoException, DriverException {
		Categories localizado = null;
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_BY_CODIGO);
			this.statement.setInt(1, _categoryid);
			this.rs = this.statement.executeQuery();

			if (rs.next()) {
				
				int CategoryID = this.rs.getInt(1);
				String CategoryName = this.rs.getString(2);
				String Description = this.rs.getString(3);
				Blob Picture = this.rs.getBlob(4);
				
				localizado = new Categories(CategoryID, CategoryName,Description, Picture);
				
			}

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
		return localizado;
	}

}
